#version 430
layout(binding=0) uniform sampler2D shadowMap;
layout(binding=1) uniform sampler2D diffuseTex;


in vec3 lightDir;
in vec3 viewDir;
in vec3 normalDir;
in vec4 shadowCoord;

in vec2 uv;
out vec4 color;
 
void main(){
	vec3 normal = normalize(normalDir);
	vec3 light = normalize(lightDir);
	vec3 view = normalize(viewDir);
	vec3 halfVec = normalize((light + view));

	//dot products
	float nDotL = clamp(dot(normal, light), 0.0, 1.0);
	float nDotH = clamp(dot(normal, halfVec), 0.0, 1.0);

	float specularCoefficient = pow(nDotH, 5.0);
	vec3 albedo = texture(diffuseTex, uv.xy).rgb;
	vec3 ambient = albedo * vec3(0.1, 0.1, 0.1);
	vec3 diffuse = albedo * vec3(0.4, 0.4, 0.4) * nDotL;
    vec3 specular = specularCoefficient * albedo * vec3(0.8, 0.8, 0.8) * vec3(1, 1, 1);

    color = vec4(ambient + diffuse + specular, 1.0);

	float visibility = 1.0;
	float shadowMapDepth = texture(shadowMap, shadowCoord.xy).z;
	if (shadowMapDepth < shadowCoord.z - 0.005){
		visibility = 0.5;
	}

	color = vec4(color.rgb * visibility, color.a);
}